// debugger;
function binarySearch(nums: number[], l: number, r: number, target: number): number {
    if (l > r) {
        return 0;
    }
    // number不会自动向下取整
    let center: number = Math.floor((l + r) / 2);

    if (nums[center] < target) {
        l = center + 1;
        return binarySearch(nums, l, r, target);
    }
    if (nums[center] > target) {
        r = center - 1;
        return binarySearch(nums, l, r, target);
    }

    if (nums[center] == target) {
        let result = 1;
        // 前遍历
        let forehead = center - 1;
        while (forehead >= 0 && nums[forehead] == target) {
            result++;
            forehead--;
        }
        // 后遍历
        let afterhead = center + 1;
        while (afterhead < nums.length && nums[afterhead] == target) {
            result++;
            afterhead++;
        }
        return result;
    }
    return 0;
}

function search_Binary(nums: number[], target: number): number {

    return binarySearch(nums, 0, nums.length - 1, target);
};

console.log(search_Binary([1], 1));